package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.cartDao;
import com.qf.xiaomi.pojo.TbCart;
import com.qf.xiaomi.pojo.TbCartList;
import com.qf.xiaomi.pojo.TbGoods;
import com.qf.xiaomi.utils.DButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class cartDaoImpl implements cartDao {
    QueryRunner queryRunner = new QueryRunner(DButils.getdataSource());
    @Override
    public void addCart(long userId, long goodId) {
        String sql1="select * from tb_cart where id=? and pid=?";
        try {
            Object query = queryRunner.query(sql1, new ScalarHandler(), userId, goodId);
            if (query==null){
                String sql2 = "insert into tb_cart (id,pid,Num) values (?,?,1)";
                int update = queryRunner.update(sql2, userId, goodId);
            }else {
                String sql3="update tb_cart set Num=Num+1 where (id=?) and(pid=?)";
                int update = queryRunner.update(sql3, userId, goodId);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<TbCart> selectCartByUid(long uid, TbCartList tbCartList) {
        String sql="select * from tb_cart where id = ?";
        try {
            List<TbCart> tbCart1s = queryRunner.query(sql, new BeanListHandler<>(TbCart.class),uid);
            return tbCart1s;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<TbGoods> selectByCart(TbCart tbCart) {
        String sql="select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tg.id=?";
        try {
            List<TbGoods> tbGoods = queryRunner.query(sql, new BeanListHandler<>(TbGoods.class), tbCart.getId());
            return tbGoods;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int[] selectNumsByCart(TbCart tbCart) {
        String sql = "select Num from t_cart where id=?";
        try {
            List<TbCart> result = queryRunner.query(sql, new BeanListHandler<>(TbCart.class), tbCart.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return new int[0];
    }

    @Override
    public void del(long cid) {
        String sql="delete from tb_cart where pid = ?";
        try {
            int update = queryRunner.update(sql, cid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int clearCart() {
        String sql="delete from tb_cart ";
        try {
            int update = queryRunner.update(sql);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
